package com.david.appletree;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;

public class JCalendarUtil {
    private static ArrayList<String> weeks = new ArrayList<>();
    public static void main(String args[]) {
        weeks.add("星期一");
        weeks.add("星期二");
        weeks.add("星期三");
        weeks.add("星期四");
        weeks.add("星期五");
        weeks.add("星期六");
        weeks.add("星期日");

        System.out.print("请选择年份(大于1990年)：");

        Scanner input = new Scanner(System.in);
        int year = input.nextInt();

        System.out.print("请选择该年的月份：");
        int month = input.nextInt();



        ArrayList<WeekDay> weekDays = getDays(year, month);


        printWeek();

        printDays(weekDays);
    }


    /**
     * 获取对应月份的日历表。
     * @param year 要获取的年份， 如：2022
     * @param month 要获取的月份， 如：三月份，传3
     * @return 返回的当月日历表
     */
    private static ArrayList<WeekDay> getDays(int year, int month){
        ArrayList<WeekDay> weekDays = new ArrayList<>();


        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);//月份从0开始，所以减1
        calendar.set(Calendar.DATE, 1);//设置为1号



        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int weekDay = (calendar.get(Calendar.DAY_OF_WEEK));//周日对应0， 周六对应6;当要周一对应0时，减2即可

        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        System.out.println("days:" + calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        System.out.println("weekDay:" + (calendar.get(Calendar.DAY_OF_WEEK) - 2));//从周日开始且起始索引为1


        int chargeNum = Calendar.SATURDAY + 1;
        for (int i = 1;i<= days;++i) {
            weekDays.add(new WeekDay(weekDay, i));
            ++weekDay;
            if (weekDay == chargeNum) {
                weekDay = Calendar.SUNDAY;
            }
        }
        return weekDays;
    }


    private static void printDays(ArrayList<WeekDay> days){
        //打印从周日到周六时
//        for (int i = 0; i < days.get(0).week - Calendar.MONDAY; i++) {
//            System.out.print(" - ");
//        }
//
//        for (int i = 0;i<days.size();++i) {
//            System.out.print(" " + days.get(i).date);
//            if (days.get(i).week == Calendar.SATURDAY) {
//                System.out.println();
//            }
//        }

        //打印从周一到周日时
        if (days.get(0).week == Calendar.SUNDAY) {
            for (int i = 0; i < 6; i++) {
                System.out.print(" - ");
            }
        } else {
            for (int i = 0; i < days.get(0).week - Calendar.MONDAY; i++) {
                System.out.print(" - ");
            }
        }
        for (int i = 0;i<days.size();++i) {
            System.out.print(" " + days.get(i).date);
            if (days.get(i).week == Calendar.SUNDAY) {
                System.out.println();
            }
        }
    }

    private static void printWeek(){
        for (int i = 0; i < 7; i++) {
            if (i == 0) {
                System.out.print(weeks.get(i));
            } else {
                System.out.print("   " + weeks.get(i));
            }
        }
        System.out.println();
    }

    static class WeekDay{
        /**
         * 值为：
         * {@link java.util.Calendar#SUNDAY}
         * {@link java.util.Calendar#MONDAY}
         * {@link java.util.Calendar#TUESDAY}
         * {@link java.util.Calendar#WEDNESDAY}
         * {@link java.util.Calendar#THURSDAY}
         * {@link java.util.Calendar#FRIDAY}
         * {@link java.util.Calendar#SATURDAY}
         *
         * 从周一开始到周日时，前面可填充1号的week - MONDAY个值，遇SUNDAY时换行(注：当1号为周日值为1时直接添加6个空日期)；
         * 从周日开始到周六时，前面可填充1号的week - SUNDAY个值，遇SATURDAY时换行；
         */
        int week;
        int date;

        public WeekDay() {
        }

        public WeekDay(int week, int date) {
            this.week = week;
            this.date = date;
        }
    }
}
